<template>
  <div class="app-container">
    <el-table v-loading="listLoading" :data="noticeList" element-loading-text="Loading" border fit highlight-current-row>
      <el-table-column label="公告ID" width="120" align="center">
        <template slot-scope="scope">
          {{ scope.row.id }}
        </template>
      </el-table-column>
      <el-table-column label="公告标题" width="200" align="center">
        <template slot-scope="scope">
          {{ scope.row.title }}
        </template>
      </el-table-column>
      <el-table-column label="公告内容" width="200" align="center">
        <template slot-scope="scope">
            <p class="ellipsis"> {{ scope.row.content }}</p>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" width="180" align="center">
        <template slot-scope="scope">
          {{ scope.row.createTime }}
        </template>
      </el-table-column>
      <el-table-column label="修改时间" width="180" align="center">
        <template slot-scope="scope">
          {{ scope.row.updateTime }}
        </template>
      </el-table-column>
      <el-table-column label="状态" width="100" align="center">
        <template slot-scope="scope">
          <el-tag type="success" v-if="scope.row.status == 1">正常</el-tag>
          <el-tag type="danger" v-else-if="scope.row.status == 2">已删除</el-tag>
          <el-tag type="danger" v-else>状态异常</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="200">
        <template slot-scope="scope">
          <el-button type="primary" size="mini" @click="handleEdit(scope.row)">编辑</el-button>
          <el-button size="mini" v-if="scope.row.status == 1" type="danger" @click="open(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="page">
      <el-pagination background layout="prev, pager, next" :total="requestParams.total" @current-change="change"
        :page-size="requestParams.pageSize">
      </el-pagination>
    </div>

    <!-- el-dialog -->
    <el-dialog width="35%" title="编辑" :visible.sync="dialogFormVisible">
      <el-form :model="noticeForm">
        <el-form-item label="公告标题" :label-width="formLabelWidth">
          <el-input v-model="noticeForm.title" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="公告内容" :label-width="formLabelWidth">
          <el-input type="textarea" :rows="15" v-model="noticeForm.content" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>

      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" :loading="buttonLoading" @click="SubmitEvent">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getNoticeList, updateNotice, deleteById } from "@/api/notice";
export default {
  data() {
    return {
      noticeList: null,
      listLoading: false,

      noticeForm: {
        title: "",
        content: ""
      },
      requestParams: {
        pageNo: 1,
        pageSize: 10,
        total: 0,
      },
      dialogFormVisible: false,
      buttonLoading: false,
      formLabelWidth: "120px",
    };
  },
  created() {
    this.fetchData();
  },
  methods: {
    fetchData() {
      this.listLoading = true;
      // 假设 getNotices 是获取公告列表的API方法
      getNoticeList(this.requestParams).then(response => {
        this.noticeList = response.data.list;
        this.requestParams.total = response.data.total;
        this.listLoading = false;
      });
    },

    change(page) {
      this.requestParams.pageNo = page;
      this.fetchData();
    },

    //删除
    open(row) {
      this.$confirm('此操作将永久删除公告, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteById(row.id).then(res => {
          this.$message({
            type: 'success',
            message: '删除成功!'
          });
        }).catch(err => {

        })

      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    },

    handleEdit(row) {
      this.dialogFormVisible = true
      this.noticeForm.id = row.id
      this.noticeForm.title = row.title
      this.noticeForm.content = row.content
    },
    SubmitEvent() {
      this.buttonLoading = true
      updateNotice(this.noticeForm).then(res => {
        this.$message({
          type: 'success',
          message: '修改成功!'
        });
      }).catch(err => {

      })
      this.fetchData()
      this.buttonLoading = false
      this.dialogFormVisible = false
    }
  }
};
</script>

<style scoped>
.page {
  margin-top: 15px;
  text-align: center;
}

.ellipsis {
  -webkit-line-clamp: 2;
  display: -webkit-box;
  -webkit-box-orient: vertical;
  overflow: hidden;
  text-overflow: ellipsis;
}
</style>